layui.use("layer");

var feijin = {
    get: function (option) {
        var load = layer.load(2);
        if (!option.async) {
            option.async = true;
        }
        $.ajax({
            url: option.url,
            type: "GET",
            async: option.async,
            dataType: "json",
            data: option.data,
            success: function (res) {
                layer.close(load);
                if (res.status) {
                    if (option.success && typeof option.success === "function") {
                        option.success(res);
                    } else {
                        layer.alert(res.msg || res.message || "成功", function () {
                            location.reload();
                        })
                    }
                } else {
                    layer.alert(res.msg || res.message || "失败");
                }
            },
            error: function (XMLHttpRequest) {
                layer.close(load);
                layer.alert("请求异常：" + XMLHttpRequest.status);
            }
        })
    },
    post: function (option) {
        var load = layer.load(2);
        if (!option.async) {
            option.async = true;
        }
        $.ajax({
            url: option.url,
            type: "POST",
            async: option.async,
            dataType: "json",
            data: option.data,
            success: function (res) {
                layer.close(load);
                if (res.status) {
                    if (option.success && typeof option.success === "function") {
                        option.success(res);
                    } else {
                        layer.alert(res.msg || res.message || "成功", function () {
                            location.reload();
                        })
                    }
                } else {
                    layer.alert(res.msg || res.message || "失败");
                }
            },
            error: function (XMLHttpRequest) {
                layer.close(load);
                layer.alert("请求异常：" + XMLHttpRequest.status);
            }
        })
    },
    put: function (option) {
        var load = layer.load(2);
        if (!option.async) {
            option.async = true;
        }
        option.data["_method"] = "PUT";
        $.ajax({
            url: option.url,
            type: "POST",
            async: option.async,
            dataType: "json",
            data: option.data,
            success: function (res) {
                layer.close(load);
                if (option.success && typeof option.success === "function") {
                    option.success(res);
                } else {
                    if (res.status) {
                        layer.alert(res.msg || res.message || "成功", function () {
                            location.reload();
                        });
                    } else {
                        layer.alert(res.msg || res.message || "失败");
                    }
                }
            },
            error: function (XMLHttpRequest) {
                layer.close(load);
                layer.alert("请求异常：" + XMLHttpRequest.status);
            }
        })
    },
    delete: function (option) {
        var load = layer.load(2);
        if (!option.async) {
            option.async = true;
        }
        option.data["_method"] = "DELETE";
        $.ajax({
            url: option.url,
            type: "POST",
            async: option.async,
            dataType: "json",
            data: option.data,
            success: function (res) {
                layer.close(load);
                if (res.status) {
                    if (option.success && typeof option.success === "function") {
                        option.success(res);
                    } else {
                        layer.alert(res.msg || res.message || "成功", function () {
                            location.reload();
                        });
                    }
                } else {
                    layer.alert(res.msg || res.message || "失败");
                }
            },
            error: function (XMLHttpRequest) {
                layer.close(load);
                layer.alert("请求异常：" + XMLHttpRequest.status);
            }
        })
    }
};